local skill = fk.CreateSkill {
  name = "#benevolence__precept_skill",
  attached_equip = "benevolence__precept",
}

Fk:loadTranslationTable{
  ["#benevolence__precept_skill"] = "仁戒·贫律",
  ["@@benevolence__precept-turn"] = "仁戒·贫律",
  ["#benevolence__precept-invoke"] = "是否废除防御坐骑栏？",
  ["#benevolence__precept-show"] = "仁戒·贫律：请明置两张手牌",
  ["#benevolence__precept-choose"] = "仁戒·贫律：将 %dest 明置的牌交给另一名角色",
}

local DIY = require "packages/diy_utility/diy_utility"

skill:addEffect(fk.AfterCardsMove, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skill.name) and
      table.every(player.room.alive_players, function (p)
        return p:getHandcardNum() <= player:getHandcardNum()
      end) then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Card.PlayerHand then
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(player:getEquipments(Card.SubtypeDefensiveRide), function (id)
      return Fk:getCardById(id).name == skill.attached_equip
    end)
    if #cards > 0 then
      room:throwCard(cards, skill.name, player, player)
    end
  end,
})

skill:addEffect(fk.AfterCardsMove, {
  priority = 0,
  can_trigger = function(self, event, target, player, data)
    if player.room:getBanner("hanqing__gujie") and
      table.find(player.room.void, function (id)
        return Fk:getCardById(id).name == skill.attached_equip
      end) then
      for _, move in ipairs(data) do
        if move.from and move.toArea == Card.Void then
          if move.from == player and not player.dead then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerEquip and
                Fk:getCardById(info.cardId).name == skill.attached_equip then
                return true
              end
            end
          end
        end
      end
    end
    if table.contains(player.sealedSlots, Player.DefensiveRideSlot) and player.room:getBanner("hanqing__duoye") and
      table.every(player.room.alive_players, function (p)
        return p:getHandcardNum() <= player:getHandcardNum()
      end) then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Card.PlayerHand then
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = DIY.askToShowCards(player, {
      min_num = 2,
      max_num = 2,
      skill_name = skill.name,
      prompt = "#benevolence__precept-show",
      cancelable = false,
    })
    if player.dead then return end
    cards = table.filter(cards, function (id)
      return table.contains(player:getCardIds("h"), id)
    end)
    if #cards > 0 and #room:getOtherPlayers(player, false) > 0 then
      for _, p in ipairs(room.players) do
        if p.general == "godshangyang" or p.deputyGeneral == "godshangyang" then
          local to = room:askToChoosePlayers(player, {
            min_num = 1,
            max_num = 1,
            targets = room:getOtherPlayers(player, false),
            skill_name = skill.name,
            prompt = "#benevolence__precept-choose::"..player.id,
            cancelable = false,
          })[1]
          room:moveCardTo(cards, Card.PlayerHand, to, fk.ReasonGive, skill.name, nil, true, player)
          if player.dead then return end
          break
        end
      end
    end
    if #player:getAvailableEquipSlots(Card.SubtypeDefensiveRide) > 0 and
      room:askToSkillInvoke(player, {
        skill_name = skill.name,
        prompt = "#benevolence__precept-invoke",
      }) then
      room:abortPlayerArea(player, Player.DefensiveRideSlot)
    end
  end,
})

skill:addEffect(fk.BeforeCardsMove, {
  can_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip and
            Fk:getCardById(info.cardId).name == skill.attached_equip then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local new_data = {}
    for _, move in ipairs(data) do
      local new_move = {}
      if #move.moveInfo > 0 and move.toArea ~= Card.Void then
        local orig_info, new_info = {}, {}
        for i = 1, #move.moveInfo do
          local info = move.moveInfo[i]
          local will_destruct = false
          if info.fromArea == Card.PlayerEquip and Fk:getCardById(info.cardId).name == skill.attached_equip then
            will_destruct = true
          end
          if will_destruct then
            table.insert(new_info, info)
          else
            table.insert(orig_info, info)
          end
        end
        move.moveInfo = orig_info
        if #new_info > 0 then
          new_move = {
            moveInfo = new_info,
            from = player,
            to = nil,
            toArea = Card.Void,
            moveReason = fk.ReasonJustMove,
            proposer = nil,
            skillName = skill.name,
            moveVisible = true,
          }
        end
      end
      if next(new_move) then
        table.insert(new_data, new_move)
      end
    end
    if next(new_data) then
      table.insertTable(data, new_data)
    end
  end,
})

return skill
